package ExerciseClass.Exercise33;

import java.io.*;

public class Exercise1 {
    public static void main(String[] args)  {
        //加密并写到磁盘
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(new File("F:\\123.txt"));
            FileOutputStream fileOutputStream= new FileOutputStream(new File("F:\\1234.txt"));

            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            byte []arr = new byte[1024];
            int len;
            while ((len=bufferedInputStream.read(arr))!=-1){
                for(int i = 0 ; i <len;i++){
                    arr[i] = (byte) (arr[i] ^5);
                }
                bufferedOutputStream.write(arr,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bufferedInputStream!=null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedOutputStream!=null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //读取并解密：
        BufferedInputStream bufferedInputStream1=null;
        try {
            FileInputStream FileInputStream = new FileInputStream(new File("F://1234.txt"));
            bufferedInputStream1  = new BufferedInputStream(FileInputStream);
            String str=null;
            int write;
            byte [ ] arr1 = new byte[1024];
            while ((write=bufferedInputStream1.read(arr1))!=-1){
                for (int i = 0 ; i<write;i++){
                    arr1[i] = (byte) (arr1[i] ^5);
                }
                str = new String(arr1,0,write);
            }
//            解密后
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedInputStream1!=null){
                try {
                    bufferedInputStream1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
